package controller;

import helper.AbstractAction;
import helper.ParseUtil;

import java.awt.Frame;
import java.awt.event.ActionEvent;

import javax.swing.JOptionPane;

import view.AnlegenKundeDialog;
import bank.Kunde;


public class AnlegenKundenDialogController {

    private final BankHandler bankHandler;
    private final Frame parent;

    private AnlegenKundeDialog dialog;


    public AnlegenKundenDialogController(BankHandler aBankHandler, Frame aParent) {
        bankHandler = aBankHandler;
        parent = aParent;
    }

    public void start() {
        dialog = new AnlegenKundeDialog(parent, true);
        dialog.getBtAnlegen().addActionListener(new AnlegenKundeAction());
        dialog.getBtEnde().addActionListener(new BeendenAction());
    }

    private void clearDialog() {
        dialog.getTfName().setText("");
        dialog.getTfNummer().setText("");
    }


    // Inner Classes ***********************************************************

    private class AnlegenKundeAction extends AbstractAction {
		@Override
		protected void execute(ActionEvent aEvent) throws Exception {
            int kundenNummer = ParseUtil.parseKundennummer(dialog.getTfNummer().getText());
        	String kundenName = dialog.getTfName().getText();

            Kunde neuerKunde = bankHandler.anlegenKunde(kundenName, kundenNummer);
            JOptionPane.showMessageDialog(dialog, "Kunde: " + neuerKunde.getName() + " angelegt.");
            clearDialog();
        }
    }

    private class BeendenAction extends AbstractAction {
		@Override
		protected void execute(ActionEvent aEvent) throws Exception {
            dialog.dispose();
        }
    }
}
